<template>
  <div class="app-container calendar-list-container">
    <query :createHandle="showAddDialog" :queryHandle="queryList"></query>
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
      style="width: 100%">
      <el-table-column align="center" :label="$t('table.activity.id')">
        <template slot-scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.title')">
        <template slot-scope="scope">
          <span>{{scope.row.title}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.content')">
        <template slot-scope="scope">
          <span>{{scope.row.content}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.createTime')">
        <template slot-scope="scope">
          <span>{{scope.row.createTime}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.expTime')">
        <template slot-scope="scope">
          <span>{{scope.row.expTime}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.createUserId')">
        <template slot-scope="scope">
          <span>{{scope.row.createUserId}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.index')">
        <template slot-scope="scope">
          <span>{{scope.row.index}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.startTime')">
        <template slot-scope="scope">
          <span>{{scope.row.startTime}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.isHot')">
        <template slot-scope="scope">
          <span>{{scope.row.isHot}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.img')">
        <template slot-scope="scope">
          <span>{{scope.row.img}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.price')">
        <template slot-scope="scope">
          <span>{{scope.row.price}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.personCount')">
        <template slot-scope="scope">
          <span>{{scope.row.personCount}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.surplusCount')">
        <template slot-scope="scope">
          <span>{{scope.row.surplusCount}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.addr')">
        <template slot-scope="scope">
          <span>{{scope.row.addr}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.activity.bmstartTime')">
        <template slot-scope="scope">
          <span>{{scope.row.bmstartTime}}</span>
        </template>
      </el-table-column><el-table-column align="center" :label="$t('table.actions')"  min-width="145" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">{{$t('table.edit')}}</el-button>         
          <el-button v-if="scope.row.status!='deleted'" size="mini" type="danger" @click="handleModifyStatus(scope.row,'deleted')">{{$t('table.delete')}}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  
    <editDialog  @hiddenEditDialog="hiddenEditDialog" :Visible="editFormVisible" :editData="temp" ></editDialog>
    <addDialog  @hiddenAddDialog="hiddenAddDialog" :Visible="addFormVisible" ></addDialog>
  </div>
</template>

<script>
import { activityList } from '@/api/activity'
import waves from '@/directive/waves' // 水波纹指令
import { parseTime } from '@/utils'
import editDialog from './edit'
import addDialog from './add'
import query from './query'

export default {
  name: 'activityTable',
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      editFormVisible: false,
      addFormVisible: false,
      temp: {},
      downloadLoading: false
    }
  },
  filters: {
  },
  created() {
    this.getList()
  },
  methods: {
    hiddenEditDialog() {
      this.editFormVisible = false
    },
    showAddDialog() {
      this.addFormVisible = true
    },
    hiddenAddDialog() {
      this.addFormVisible = false
    },
    queryList(option) {
      this.listQuery.page = 1
      Object.assign(this.listQuery, option)
      this.getList()
    },
    getList() {
      this.listLoading = true
      activityList(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.pagination.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    resetTemp() {
      this.temp = {
        id: '',
        title: '',
        content: '',
        createTime: '',
        expTime: '',
        createUserId: '',
        index: '',
        startTime: '',
        isHot: '',
        img: '',
        price: '',
        personCount: '',
        surplusCount: '',
        addr: '',
        bmstartTime: ''
      }
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
          // createArticle(this.temp).then(() => {
          //   this.list.unshift(this.temp)
          //   this.dialogFormVisible = false
          //   this.$notify({
          //     title: '成功',
          //     message: '创建成功',
          //     type: 'success',
          //     duration: 2000
          //   })
          // })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) //
      this.editFormVisible = true
      // this.$nextTick(() => {
      //   this.$refs['dataForm'].clearValidate()
      // })
    },
    updateData() {
    },
    handleDelete(row) {
      this.$notify({
        title: '成功',
        message: '删除成功',
        type: 'success',
        duration: 2000
      })
      const index = this.list.indexOf(row)
      this.list.splice(index, 1)
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
        const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
        const data = this.formatJson(filterVal, this.list)
        excel.export_json_to_excel(tHeader, data, 'table-list')
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  },
  components: {
    editDialog,
    query,
    addDialog
  }
}
</script>
